package com.briup.app02.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.BaseConfig;
import com.briup.app02.bean.BaseConfigExample;
import com.briup.app02.dao.BaseConfigMapper;
import com.briup.app02.service.IBaseConfigService;
import com.briup.app02.utils.CustomerException;

@Service
public class BaseConfigServiceImpl implements IBaseConfigService{
	
	@Autowired
	private BaseConfigMapper baseconfigMapper;
	
	@Override
	public List<BaseConfig> query(String name) {
		// 模板查询
		BaseConfigExample example = new BaseConfigExample();
		if(name != null) {
			example.createCriteria().andNameLike("%"+name+"%");
		}
		List<BaseConfig> list = baseconfigMapper.selectByExample(example);
		return list;
	}

	@Override
	public void saveOrUpdate(BaseConfig baseconfig) throws CustomerException{
		if(baseconfig.getId() == null) {
			baseconfigMapper.updateByPrimaryKey(baseconfig);
		} else {
			//判断是否有同名分类，有则抛出异常
			BaseConfigExample example = new BaseConfigExample();
			example.createCriteria().andNameEqualTo(baseconfig.getName());
			List<BaseConfig> list = baseconfigMapper.selectByExample(example);
			if(list.size()>0) {
				baseconfigMapper.updateByPrimaryKey(baseconfig);
			}
		}
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseConfig baseconfig = baseconfigMapper.selectByPrimaryKey(id);
		if (baseconfig == null ) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		baseconfigMapper.deleteByPrimaryKey(id);
	}

}
